package demo_Book;

import java.util.Scanner;

public class Server   {
    public Scanner scanner = new Scanner(System.in);
    private Book[] books;
    private int count;
    Book book;
    public Server() {
        books = new Book[100];
        count = 0;
    }
    //BLL
    // 添加书籍
    public void addBook(Book book) {
            books[count++] = book;
            System.out.println("书籍添加成功。");
    }
    //查找书籍
    public  Book FinBookCode(String FinBookCode){
        for (int i = 0; i < count; i++) {
            if (books[i].getBookCode().equals(FinBookCode)) {
                return books[i];
            }
        }
        return null;
    }
    // 更新书籍信息  遵循单一职责原则的代码实现
    public void updateBook(String bookCode, String title, String author, String price) {
        Book book = FinBookCode(bookCode);
            if (book!=null) {
                book.setTitle(title);
                book.setAuthor(author);
                book.setPrice(price);
                System.out.println("书籍已更新。");
            }
    }
    // 删除书籍
    public void deleteBook(String bookCode) {
        for (int i = 0; i < count; i++) {
            if (books[i].getBookCode().equals(bookCode)) {
                for (int j = i; j < count - 1; j++) {
                    books[j] = books[j + 1];
                }
                count--;
                break;
            }
        }
    }
    // 打印所有书籍信息
    public void printAllBooks() {
        System.out.println("------------查看所有书籍信息-----------");
        System.out.println("书籍编号 \t   书籍名   \t   作者   \t   价格 ");
        for (int i = 0; i < count; i++) {
            Book house = books[i];
            System.out.println(house.getBookCode() + "     \t" + house.getTitle() + "     \t" + house.getAuthor()
                    + "     \t" + house.getPrice() + "     \t");
        }
    }
    // 根据书籍编号获取书籍
    public Book getBookByCode(String bookCode) {
        for (Book book : books) {
            if (book.getBookCode().equals(bookCode)) {
                return book;
            }
        }
        return null;
    }
    //DAL
    //    class Book AdminServer{}
   public void addBook() {
        System.out.println("------------添加书籍------------");
        System.out.println("请输入书籍编号：");
        String bookCode = scanner.nextLine();
        System.out.println("请输入书名：");
        String title = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        String price = scanner.nextLine();
        Book book = new Book(bookCode,title,author,price);
      addBook(book);
       //       server.addBook(book);
    }

    public void updateBook() {
        scanner.nextLine();
        System.out.println("请输入要修改的书籍编号：");
        String bookCode = scanner.nextLine();
        System.out.println("请输入新的书名：");
        String title = scanner.nextLine();
        System.out.println("请输入新的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入新的价格：");
        String price = scanner.nextLine();
        updateBook(bookCode, title, author, price);
        //     server.updateBook(bookCode, title, author, price);
        System.out.println("书籍信息修改成功！");
    }

    public void deleteBook() {
        System.out.println("请输入要删除的编号：");
        String BookCode = scanner.next();
        System.out.println("是否确认删除（Y/N）:请小心选择：");
        String FN=scanner.nextLine();
        String ys=scanner.nextLine();
        if (ys.equals("Y") ||ys.equals("y")){
           deleteBook(BookCode);
            //        server.deleteBook(BookCode);
            System.out.println("-------------ys 删 除 成 功！----------------");
        }
        if (FN==("Y") ||FN==("y")){
            deleteBook(BookCode);
            //     server.deleteBook(BookCode);
            System.out.println("-------------FN 删 除 成 功！----------------");
        }
        System.out.println("答应选择结果"+FN+ys);

    }

    //    class Book UserServer{}
    public void addToCart() {
        System.out.println("请输入要添加的书籍编号：");
        String bookCode = scanner.next();
        book= getBookByCode(bookCode);
        System.out.println("已将书籍编号为： "+book.getBookCode() + "  添加到购物车！");
        if (book != null) {
            System.out.println("书籍已添加到购物车。");
        } else {
            System.out.println("未找到匹配的书籍编号。");
        }
    }




}

